import { Command } from '../constant';
import Taro from '@tarojs/taro';
import { Dispatch } from 'typings';
import { IAllReducerProps } from '../types';
import { getReducerData } from '@/redux/store';
import { extraPathsValue } from '@/redux/util';
import api from 'api';

export default (dispatch: Dispatch) => {
  let action = {
    commonChange(...param: any) {
      dispatch({
        type: Command.commonChange,
        payload: extraPathsValue(...arguments),
      });
    },
    /**
     * 活动列表
     */
    async findeActiveList() {
      let { pageNum, activeList } = getData().main;
      let selectDistance = Taro.getStorageSync('selectDistance');
      let { user } = Taro.getStorageSync('b2b-wechat@login');
      let params = {
        registerBy: user?.id || '',
        pageNum,
        communityCode: selectDistance.code,
      };
      action.commonChange('main.isLoading', true);
      let res = await api.pagesIndexController.getAppFindPageList(params);
      if (res.code == '200') {
        if (pageNum == 1) {
          action.commonChange('main.activeList', res.data.rows);
          action.commonChange('main.totalPages', res.data.total);
        } else {
          let newList = activeList.concat(res.data.rows);
          action.commonChange('main.activeList', newList);
          action.commonChange('main.totalPages', res.data.total);
        }
      }
      action.commonChange('main.isLoading', false);
    },
    /**
     * 活动类型
     */
    async getActiveKind() {
      let res = await api.pagesIndexController.getActivityType();
      if (res.code == '200') {
        dispatch({
          type: Command.init,
          payload: {
            main: {
              activeType: res.data,
            },
          },
        });
      }
    },
  };
  return action;
};

function getData(): IAllReducerProps {
  return {
    main: getReducerData('packageCActiveListMain'),
  };
}

//create by moon https://github.com/creasy2010/moon
